// 这里判断好后，点击注册按钮时就不需要判断了
$("#username").keyup(function (evt) {
    document.getElementById("usernameJudge").className = "";
    $("#username").css("color", "#929999");
});

// 当用户名失去焦点时进行判断
$("#username").blur(function (evt) {
    // 用户名不为空时，进行判断
    if ($("#username").val() !== "") {
        if (judgeUsername($("#username").val())) {
            $.ajax({
                type: "GET",
                url: "/user/judge",
                data: {userName: $("#username").val().trim()},
                dataType: "json",
                success: function (response) {
                    if (undefined !== response && response.success === true) {
                        if (response.data.status) {
                            $("#username").css("color", "red");
                            document.getElementById("usernameJudge").className = "n";
                        } else {
                            $("#username").css("color", "#929999");
                            document.getElementById("usernameJudge").className = "y";
                        }
                    } else {
                        layer.alert("网络异常，请刷新重试...")
                    }
                }
            })
        }else {
            layer.alert("用户名格式错误。只允许字母开头的字母、数字和下划线的组合。", function (index) {
                layer.close(index);
                $("#username").val("");
                $("#username")[0].focus();
                document.getElementById("passwordSafe").className = "";
                document.getElementById("passwordSame").className = "";
            });
        }
    }
});

// 当第一个密码输入框按下键盘时进行判断
$("#password").keyup(function (evt) {
    // 判断密码强度
    if ($("#password").val() !== "") {
        var judge = checkPassWord($("#password").val());
        document.getElementById("passwordSafe").className = "s" + judge;
    }else {
        document.getElementById("passwordSafe").className = "";
        document.getElementById("passwordSame").className = "";
    }
});

// 当第一个密码输入框失去焦点时进行判断
$("#password").blur(function (evt) {
    var passWord = $("#password");
    var passWord2 = $("#password2");
    // 判断密码强度
    if (passWord.val() !== "") {
        var judge = checkPassWord(passWord.val());
        if (judge < 2) {
            layer.alert("密码强度太低，请重新输入！", function (index) {
                layer.close(index);
                passWord.val("");
                passWord[0].focus();
                passWord2.val("");
                document.getElementById("passwordSafe").className = "";
                document.getElementById("passwordSame").className = "";
            });
        }
    }else {
        passWord2.val("");
    }
});

// 当第一个密码输入框按下键盘时进行判断
$("#password2").keyup(function (evt) {
    // 判断密码强度
    var passWord = $("#password").val();
    var passWord2 = $("#password2").val();
    if (passWord === passWord2) {
        document.getElementById("passwordSame").className = "r";
    }else {
        document.getElementById("passwordSame").className = "w";
    }
});
// 当第二个密码输入框失去焦点时进行判断
$("#password2").blur(function (evt) {
    // 和第一个密码框比较是否相同

});


// 进行注册请求
function register() {
    if (!registerValidate()) {
        return false;
    }
    $.ajax({
        type: "POST",
        url: "/register",
        data: {userName: $("#username").val(), password: $("#password").val()},
        dataType: "json",
        success: function (data) {
            // 返回数据了，说明用户名和密码都是合法的，可以注册
            if (undefined !== data && data.success === true) {
                window.location.href = '/login';
                layer.msg("注册成功！", {icon: 1});
            } else {
                layer.msg("注册失败，请重新注册！", {icon: 7});
                $("#password").val("");
                $("#password2").val("");
                document.getElementById("passwordSafe").className = "";
                document.getElementById("passwordSame").className = "";
            }
        }
    })
}

// 校验用户名和密码，同时有效的话，并将密码进行MD5转码
function registerValidate() {
    // 校验用户名
    var goodUser = false;
    $.ajax({
        type: "GET",
        url: "/user/judge",
        async: false,
        data: {userName: $("#username").val().trim()},
        dataType: "json",
        success: function (response) {
            if (undefined !== response && response.success === true) {
                if (response.data.status) {
                    $("#username").css("color", "red");
                    document.getElementById("usernameJudge").className = "n";
                    layer.alert("该用户已经存在，请换个用户名吧！", { icon: 7 });
                } else {
                    $("#username").css("color", "#929999");
                    document.getElementById("usernameJudge").className = "y";
                    goodUser = true;
                }
            } else {
                layer.alert("网络异常，请刷新重试...")
            }
        }
    });
    if (!goodUser) {
        return goodUser;
    }

    var passWord = $("#password");
    var passWord2 = $("#password2");
    if ($("#username").val() === "") {
        layer.alert("用户名不能为空。");
        passWord.val("");
        passWord[0].focus();
        passWord2.val("");
        document.getElementById("passwordSafe").className = "";
        document.getElementById("passwordSame").className = "";
        return false;
    }
    if ($("#username").length < 4) {
        layer.alert("用户名不能短于4个字符。");
        passWord.val("");
        passWord[0].focus();
        passWord2.val("");
        document.getElementById("passwordSafe").className = "";
        document.getElementById("passwordSame").className = "";
        return false;
    }
    if (passWord.val() !== passWord2.val()) {
        layer.alert("两次输入的密码不一样，请重新输入！");
        passWord.val("");
        passWord[0].focus();
        passWord2.val("");
        document.getElementById("passwordSafe").className = "";
        document.getElementById("passwordSame").className = "";
        return false;
    }
    // 这里表示校验完毕，可以
    document.getElementById("passwordSafe").className = "";
    document.getElementById("passwordSame").className = "";
    var mm = $.trim(passWord.val());
    var md5value = $.md5(mm);
    passWord.val(md5value);

    var mm2 = $.trim(passWord2.val());
    var md5value2 = $.md5(mm2);
    passWord2.val(md5value2);
    return true;
}

// 设置全局监听事件
document.onkeydown = function (ev) {
    var e = ev || window.ev || arguments.callee.caller.arguments[0] || e.which;
    if (e && e.keyCode === 13) {
        register();
    }
};